package com.example.leaderapplication.Widgets;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.leaderapplication.R;

public class LoadingDialog extends Dialog {

    private static LoadingDialog loadingDailog;

    public static LoadingDialog getInstence(Context context) {
        if (loadingDailog == null) {
            loadingDailog = new LoadingDialog(context, R.style.MyDialogStyle);
        }
        return loadingDailog;
    }

    public LoadingDialog(@NonNull Context context) {
        this(context, 0);
    }

    public LoadingDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected LoadingDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder {
        private Context context;
        private String message;
        private boolean isShowMessage = true;
        private boolean isCancelable = true;
        private boolean isCancelOutside = true;


        public Builder(Context context) {
            this.context = context;
        }


        // 设置提示信息
        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        //设置是否提示信息
        public Builder setShowMessage(boolean isShowMessage) {
            this.isShowMessage = isShowMessage;
            return this;
        }

        // 设置是否可以按返回键取消

        public Builder setCancelable(boolean isCancelable) {
            this.isCancelable = isCancelable;
            return this;
        }

        // 设置是否可以取消
        public Builder setCancelOutside(boolean isCancelOutside) {
            this.isCancelOutside = isCancelOutside;
            return this;
        }

        public LoadingDialog create() {
            LayoutInflater inflater = LayoutInflater.from(context);
            View view = inflater.inflate(R.layout.ios_dialog_loading, null);
//            if (loadingDailog == null) {
                loadingDailog = new LoadingDialog(context, R.style.MyDialogStyle);
//            }

            Animation animation = AnimationUtils.loadAnimation(context, R.anim.loadingdialog_anim);
            animation.setInterpolator(new LinearInterpolator());
            view.findViewById(R.id.progressBar1).startAnimation(animation);
            TextView msgText = (TextView) view.findViewById(R.id.tipTextView);
            if (isShowMessage) {
                msgText.setText(message);
            } else {
                msgText.setVisibility(View.GONE);
            }
            loadingDailog.setContentView(view);
            loadingDailog.setCancelable(isCancelable);
            loadingDailog.setCanceledOnTouchOutside(isCancelOutside);
            return loadingDailog;
        }
    }

    public static boolean isLiving(Activity activity) {
        if (activity == null) {
            return false;
        }
        if (activity.isFinishing()) {
            return false;
        }
        return true;
    }

    public void dimessDialog(){
        if (loadingDailog != null) {
            loadingDailog.dismiss();
        }
    }
}
